<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;

require_once 'resources/org/code/Code.class.php';

class LoginController extends CommonController
{
    public function login()
    {
        if ($input = Input::all()) {
//            dd($input);
            $code = new \Code;
            $_code = $code->get();
            if (strtoupper($input['code']) != $_code) {
                return back()->with('msg', '验证码错误');
            }
            $user = User::first();
//            dd($user);
            if ($user->user_name != $input['user_name'] || Crypt::decrypt($user->user_pass) != $input['user_pass']) {
                return back()->with('msg', '用户名或密码错误');
            }

            session(['user' => $user]);
//            dd(session('user'));
            return redirect('admin');

        }

        return view('admin.login');
    }

    public function code()
    {
        $code = new \Code();
        $code->make();
    }

    public function quit()
    {
        session(['user' => null]);
        return redirect('admin/login');
    }

    public function crypt()
    {
        $str = '123456';
        $str_p="eyJpdiI6Im91N0xmZXhQTFpjajk5MGFyTTZHUmc9PSIsInZhbHVlIjoiRWJjVzZtSlVROW5jMmUrVHFUQTF0Zz09IiwibWFjIjoiMzcyN2VjYTE2ODc3ODM2OWI4NWM0YWY4ZGIzOWIwZWRhZjBhYmZiMDkxMjkyNWNiZDk1NDU4OTIwNDAyOWY5YSJ9";

        echo Crypt::encrypt($str);
        echo "<br/>";
        echo Crypt::decrypt($str_p);
    }
}
